function [err] = epsshiftnew2d(funct,D,N,M,eps)
% epsshiftnew2d(funct, D, N, M, eps)
% 	Parameters:
% 		funct: 	function to produce the time series in question from
% 		D: 	initial displacement
% 		N:	length of time series
% 		M:	kernel lenght for filter sample
% 		eps:	amount to peturb inital displacement by at each time step 
% 			in order to make displacement a function of time
% 	
% 	Outputs:
% 		The only output of this function are the plots described below.
% 
% 	Purpose of this code:  This code is the key code for testing where Displacment is considered
% 		a function of time.  This code has several plots that can be turned on and off as needed.
% 		This code will allow for trials to be done using an interpolation based on a Taylor Series
% 		expanded to one, two, or three series  plots can be made of these shifts and of the 
% 		fractional error when these shifts are compared to a shift made by analytic means.  This
% 		code can also generate what is being termed a "slow shift" where each term is generated by
% 		a different filter perturbed appropriately for that term.  The result fo this "slow shift"
% 		can also be plotted and its fractional error vs. the analytic shift can be plotted as well.
% 		Finally, this code can be used to build specific filters and to plot them (NOTE: this portion
% 		of the code is shaky and unproven at this time).
hold off;
%% Create Time Sequence
mesh = 1:N;
ts = funct(mesh);

%% Plot Time Series
%plot(mesh,ts,'g');
%hold on;

%% Create Analytically Shifted Time Sequence
for j = 1:N
        asmesh(j) = j - (D + (j-1).*eps);
        %asmesh(j) = mesh(j) - (D + eps);
end
for j = 1:N
    delta(j) = (D + (j - 1).*eps) - D;
end
ts2 = funct(asmesh);
%plot(mesh,delta,'g');
%plot(mesh,ts2,'r');
%hold on;

%% Create Analytically Shifted Time Sequence
%for j = 1:N
%    tsa(j) = testsine((j-D-1)+(j-+D-1).*eps);
%end

%plot(mesh,tsa,'m');

%% Create Shifted Time Sequence by Interpolation
F1 = FDtesteps(ts,D,M,@sinc);
F2 = FDtesteps(ts,D,M,@sincdir);
F3 = FDtesteps(ts,D,M,@sinc2dir);

for j = 1:N
    tsint0(j) = F1(j);
    tsint1(j) = F1(j) + delta(j).*F2(j);
    tsint2(j) = F1(j) + delta(j).*F2(j) + (1./2).*(delta(j)^2).*F3(j);
    
end

%% Building Denominators for Normalization
for j = 1:N
    Dadj = D - (M-1)/2; 
    Dint = round(Dadj); 
    k = (-(M-1)/2):1:((M-1)/2); 
    s = (Dadj-Dint)-k; 
    w = getWindow('blackman3',M,D);
    p = sinc(s).*w;
    q = sincdir(s).*w;
    q2 = sinc2dir(s).*w;
    
    intf0 = (p);% + eps.*q + (1./2).*(eps).^2.*q2);
    intf1 = (p + delta(j).*q);% + (1./2).*(eps).^2.*q2);
    intf2 = (p + delta(j).*q + (1./2).*(delta(j)).^2.*q2);
    %intf = (p + q);
    n0(j) = sum(intf0);    
    n1(j) = sum(intf1);    
    n2(j) = sum(intf2);
end

%% Normalize Interpolated Time Series
tsint0 = tsint0./n0;
tsint1 = tsint1./n1;
tsint2 = tsint2./n2;

%% Plot Interpolated Time Series
%plot(mesh,tsint0,'b');
%plot(mesh,tsint1,'m');
%plot(mesh,tsint2,'c');

%xlim([D + (M-1),asmesh(N)]);

%% Shift Time Series Using "Slow Method"
tsslow = slowshift(funct,N,D,M,eps);
%tsslow = fliplr(tsslow);

%% Plot Slow Shifted Time Series
%plot(mesh,tsslow,'c');

%% Pull Filter N - 100
[k,Fi] = buildspecfil(ts,D+(N-100-1)+eps,M);
%plot(k,Fi,'r');
%hold on;

%% Plot Original Filter
[k,Fi2] = buildspecfil(ts,D,M);
%plot(k,Fi2,'b');

%% Plot Filter With 1st Derivative
[k,Fi3] = buildspecfil2(ts,D,M,delta(N-1));
%plot(k,Fi3,'m');

%% Plot Filter With 2nd Derivative
[k,Fi4] = buildspecfil3(ts,D,M,delta(N-1));
%plot(k,Fi4,'c');

%% Calculating Filter Errors
ferr1 = abs(Fi - Fi2)./abs(Fi);
ferr2 = abs(Fi - Fi3)./abs(Fi);
ferr3 = abs(Fi2- Fi3)./abs(Fi2);
ferr4 = abs(Fi - Fi4)./abs(Fi);
ferr5 = abs(Fi3 - Fi4)./abs(Fi3);
%semilogy(k,ferr1,'b');
%hold on;
%semilogy(k,ferr2,'m');
%semilogy(k,ferr3,'g');
%hold on;
%semilogy(k,ferr4,'c');
%semilogy(k,ferr5,'k');

%% Calculating Error
err0 = abs(ts2 - tsint0)./abs(ts2);
err1 = abs(ts2 - tsint1)./abs(ts2);
err2 = abs(ts2 - tsint2)./abs(ts2);
%ts2=FDtest(ts,D,M);
errslow = abs(ts2 - tsslow)./abs(ts2);
%semilogy(mesh,err0,'r',mesh,err1,'g');%,mesh,err2,'b');
%semilogy(mesh,err0,'r');
semilogy(mesh,errslow,'g');
xlim([D + (M-1),mesh(N)]);
%ylim([1e-11,1e-8]);

%plot(mesh,ts2,'g',mesh,tsslow,'m');

end